home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / makeitable / makeitable.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  12.7 KB  |  502 lines

  1. /*
  2.     File:        MakeITable.c
  3.  
  4.     Contains:    This is a very boring app which just forces QuickDraw to update the ITable of
  5.                 all the monitors.  The Resolution will end up at 5 bits.  The ITable will change
  6.                 when you select start from the file menu.
  7.  
  8.  
  9.     Written by: RICHARD P. COLLYER     
  10.  
  11.     Copyright:    Copyright © 1987-1999 by Apple Computer, Inc., All Rights Reserved.
  12.  
  13.                 You may incorporate this Apple sample source code into your program(s) without
  14.                 restriction. This Apple sample source code has been provided "AS IS" and the
  15.                 responsibility for its operation is yours. You are not permitted to redistribute
  16.                 this Apple sample source code as "Apple sample source code" after having made
  17.                 changes. If you're going to re-distribute the source, we require that you make
  18.                 it clear in the source that the code was descended from Apple sample source
  19.                 code, but that you've made changes.
  20.  
  21.     Change History (most recent first):
  22.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  23.                 
  24.  
  25. */
  26.  
  27. #include    <CType.h>
  28. #include    <Quickdraw.h>
  29. #include    <QDOffscreen.h>
  30. #include    <Windows.h>
  31. #include    <OSUtils.h>
  32. #include    <Controls.h>
  33. #include    <desk.h>
  34. #include    <dialogs.h>
  35. #include    <Events.h>
  36. #include    <Memory.h>
  37. #include    <Menus.h>
  38. #include    <Notification.h>
  39. #include    <OSEvents.h>
  40. #include    <Packages.h>
  41. #include    <Palettes.h>
  42. #include    <Printing.h>
  43. #include    <Resources.h>
  44. #include    <SegLoad.h>
  45. #include    <Sound.h>
  46. #include    <Slots.h>
  47. #include    <ToolUtils.h>
  48. #include    <StdIO.h>
  49. #include    <math.h>
  50.  
  51. extern _DataInit();
  52.  
  53. #define    TRUE            0xFF
  54. #define    FALSE            0
  55. #define    VERSION            1
  56. #define    SR_BIT            0
  57.  
  58. #define    appleID            128            
  59. #define    appleMenu        0
  60. #define    aboutMeCommand    1
  61.  
  62. #define    fileID            129
  63. #define    startCommand    1
  64. #define    printCommand    3
  65. #define    quitCommand     5
  66.  
  67. #define    editID            130
  68.  
  69. #define    aboutMeDLOG        128
  70. #define    okButton        1
  71. #define    authorItem        2
  72. #define    languageItem    3
  73.  
  74. long                Tick, WhereBoard;
  75. int                    yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
  76. Rect                screenRect, BaseRect, TotalRect, minRect;
  77. WindowPtr            whichWindow, myWindow;
  78. CTabHandle            mycolors;
  79. PaletteHandle        srcPalette;
  80. MenuHandle            mymenu1, mymenu2, mymenu0;
  81. EventRecord         myEvent;
  82. Boolean                DoneFlag, Color, CPU20, FPU, Sys6, Memboard;
  83. GDHandle            theGDevice;
  84. SysEnvRec            theWorld;
  85. OSErr                OSys;
  86. SpBlockPtr            mySpBlockPtr;
  87. SpBlock                mySpBlock;
  88.  
  89. void start();
  90. void DrawStuff (Rect theRect);
  91. void Print();
  92. void showAboutMeDialog();
  93. void doCommand(long mResult);
  94. void init();
  95. /*______________________________________________________*/
  96. /*                What ever You want                    */
  97. /*______________________________________________________*/
  98. void start()
  99. {
  100.     GDHandle    theGDHand;
  101.     
  102.     theGDHand = GetDeviceList();
  103.     do {
  104.         HLock ((Handle) theGDHand);
  105.         if (TestDeviceAttribute (theGDHand, 13) && 
  106.                 TestDeviceAttribute (theGDHand, 15) &&
  107.                 (*theGDHand)->gdType == 0) {
  108.             (*theGDHand)->gdResPref = 5;
  109.             (*(*theGDHand)->gdITable)->iTabSeed = GetCTSeed();
  110.             GDeviceChanged (theGDHand);
  111.             HUnlock ((Handle) theGDHand);
  112.             }
  113.         } while ((theGDHand = GetNextDevice(theGDHand)) != nil);
  114.  
  115.     return;
  116. }
  117.  
  118. /*______________________________________________________*/
  119. /*                     Draw Stuff                       */
  120. /*______________________________________________________*/
  121. void DrawStuff (Rect theRect)
  122. {
  123.     SetRect (&theRect, 10, 10, 200, 200);
  124.     
  125.     CopyBits (&qd.screenBits, &qd.thePort->portBits, &qd.screenBits.bounds, 
  126.                 &qd.screenBits.bounds, srcCopy, nil);
  127.  
  128.  
  129. /**
  130.     CopyBits (&window->portBits, &window->portBits, &window->portRect, &window->portRect, srcOr, nil);
  131.     CopyBits (&windpeek->port.portBits, &windpeek->port.portBits, &window->portRect, &window->portRect, srcOr, nil);
  132.     CopyBits (&screenBits, &(*thePort).portBits, &theWorld, &theWorld, srcOr, nil);
  133. */
  134.     return;
  135. }  /* DrawStuff */
  136.  
  137. /*______________________________________________________*/
  138. /*                        Print                         */
  139. /*______________________________________________________*/
  140. void Print()
  141. {
  142.     TPPrPort    thePrPort;
  143.     TPrStatus    theStatus;
  144.     GrafPtr        oldPort;
  145.     THPrint        thePrRecHdl;
  146.     
  147.     GetPort(&oldPort);
  148.     
  149.     thePrRecHdl = (THPrint)  NewHandle (sizeof (TPrint));
  150.     
  151.     PrOpen();
  152.  
  153.         if (PrError() == noErr)
  154.          {
  155.             PrintDefault(thePrRecHdl);
  156.             
  157.             if (! PrStlDialog(thePrRecHdl)) 
  158.               ExitToShell();
  159.               
  160.             if (! PrJobDialog(thePrRecHdl)) 
  161.               ExitToShell();
  162.               
  163.             thePrPort = PrOpenDoc(thePrRecHdl, nil, nil);
  164.         
  165.             if (PrError() == noErr) 
  166.              {
  167.                 PrOpenPage(thePrPort, nil);
  168.                 if (PrError() == noErr) 
  169.                  {
  170.                     /********* Print from here ****************/
  171.                     
  172.                     DrawStuff ((**thePrRecHdl).prInfo.rPage);
  173.                  }
  174.              }
  175.             PrClosePage(thePrPort);
  176.         }
  177.         PrCloseDoc(thePrPort);
  178.     
  179.     if ((((TPPrint)*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
  180.         PrPicFile(thePrRecHdl, nil, nil, nil, &theStatus);                
  181.  
  182.     PrClose();
  183.  
  184.     SetPort(oldPort);
  185.     return;
  186. }
  187.  
  188. /*______________________________________________________*/
  189. /*                 About Prog Dialog                    */
  190. /*______________________________________________________*/
  191. void showAboutMeDialog()
  192. {
  193.     GrafPtr     savePort;
  194.     DialogPtr    theDialog;
  195.     short        itemHit;
  196.  
  197.     GetPort(&savePort);
  198.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  199.     SetPort(theDialog);
  200.  
  201.     do {
  202.         ModalDialog(nil, &itemHit);
  203.     } while (itemHit != okButton);
  204.  
  205.     CloseDialog(theDialog);
  206.  
  207.     SetPort(savePort);
  208.     return;
  209. }
  210.  
  211. /*______________________________________________________*/
  212. /*                 Do Menu Function                     */
  213. /*______________________________________________________*/
  214. void doCommand(long mResult)
  215. {
  216.     int                     theMenu, theItem;
  217.     Str255                    daName;
  218.     GrafPtr                 savePort;
  219.  
  220.     theItem = LoWord(mResult);
  221.     theMenu = HiWord(mResult);
  222.     
  223.     switch (theMenu) {
  224. /*______________________________________________________*/
  225. /*                    Do Apple Menu                     */
  226. /*______________________________________________________*/
  227.         case appleID:
  228.             if (theItem == aboutMeCommand)
  229.                 showAboutMeDialog();
  230.             else {
  231.                 GetMenuItemText(mymenu0, theItem, daName);
  232.                 GetPort(&savePort);
  233.                 (void) OpenDeskAcc(daName);
  234.                 SetPort(savePort);
  235.             }
  236.             break;
  237. /*______________________________________________________*/
  238. /*                     Do File Menu                     */
  239. /*______________________________________________________*/
  240.         case fileID:
  241.             switch (theItem) {
  242.                 case startCommand:
  243.                     start();
  244.                     break;
  245.                 case printCommand:
  246.                     Print();
  247.                     break;
  248.                 case quitCommand:
  249.                     DoneFlag = TRUE;
  250.                     break;
  251.                 default:
  252.                     break;
  253.                 }
  254.             break;
  255. /*______________________________________________________*/
  256. /*                     Do Edit Menu                     */
  257. /*______________________________________________________*/
  258.         case editID:
  259.             switch (theItem) {
  260.                 default:
  261.                     break;
  262.                 }
  263.             break;
  264.         default:
  265.             break;
  266.         }
  267.     HiliteMenu(0);
  268.     return;
  269.     
  270. }
  271.  
  272.  
  273.  
  274. /*______________________________________________________*/
  275. /*               Initialization traps                   */
  276. /*______________________________________________________*/
  277. void init()
  278. {
  279.     RgnHandle        tempRgn;
  280.     
  281.     UnloadSeg(_DataInit);
  282.     InitGraf(&qd.thePort);
  283.     FlushEvents(everyEvent, 0);
  284.     InitWindows();
  285.     InitDialogs(nil);
  286.     InitCursor();
  287. /*______________________________________________________*/
  288. /*            If not right Machine then stop            */
  289. /*______________________________________________________*/
  290. /*    OSys = SysEnvirons(VERSION,&theWorld);
  291.     if(!theWorld.hasColorQD)
  292.         Color = TRUE;
  293.     else
  294.         Color = FALSE;
  295.         
  296.     if(theWorld.processor >= env68020)
  297.         CPU20 = TRUE;
  298.     else
  299.         CPU20 = FALSE;
  300.         
  301.     if(!theWorld.hasFPU)
  302.         FPU = TRUE;
  303.     else
  304.         FPU = FALSE;
  305.         
  306.     if(theWorld.systemVersion < 0x0600)
  307.         Sys6 = TRUE;
  308.     else
  309.         Sys6 = FALSE;
  310.         
  311.     if (CPU20 && (GetTrapAddress(0xA06E) != GetTrapAddress(0xA89F))) {
  312.         mySpBlockPtr = &mySpBlock;
  313.         mySpBlockPtr->spSlot = 0;
  314.         mySpBlockPtr->spTBMask = 0x03;
  315.         mySpBlockPtr->spCategory = 9;
  316.         mySpBlockPtr->spCType = 2;
  317.                 
  318.         err = SNextTypeSRsrc (mySpBlockPtr);
  319.                 
  320.         if (err) {
  321.             mySpBlockPtr = &mySpBlock;
  322.             mySpBlockPtr->spSlot = 0;
  323.             mySpBlockPtr->spTBMask = 0x03;
  324.             mySpBlockPtr->spCategory = 9;
  325.             mySpBlockPtr->spCType = F;
  326.                 
  327.             err = SNextTypeSRsrc (mySpBlockPtr);
  328.                 
  329.             if (err)
  330.                 Memboard = FALSE;
  331.             else {
  332.                 Memboard = TRUE;
  333.                 WhereBoard = mySpBlockPtr->spSlot;
  334.                 }
  335.             }
  336.         else {
  337.             Memboard = TRUE;
  338.             WhereBoard = mySpBlockPtr->spSlot;
  339.             }
  340.         }*/
  341.                     
  342. /*______________________________________________________*/
  343. /*                     Set Rects                        */
  344. /*______________________________________________________*/
  345.     screenRect = qd.screenBits.bounds;
  346.     offLeft = 0;
  347.     offTop = 0;
  348.     offRight = screenRect.right;
  349.     offBottom = screenRect.bottom;
  350.     SetRect(&BaseRect, 40, 60, 472, 282);
  351.     tempRgn = GetGrayRgn();
  352.     HLock ((Handle) tempRgn);
  353.     TotalRect = (**tempRgn).rgnBBox;
  354.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  355.                 (**tempRgn).rgnBBox.bottom - 40);
  356.     HUnlock ((Handle) tempRgn);
  357.  
  358. /*______________________________________________________*/
  359. /*        Open Window & set Palette & Picture           */
  360. /*______________________________________________________*/
  361.     theGDevice = GetMainDevice();
  362.     HLock ((Handle) theGDevice);
  363.     mycolors = (**(**theGDevice).gdPMap).pmTable;
  364.     numcolor = (**(**theGDevice).gdPMap).pixelSize;
  365.     HUnlock((Handle) theGDevice);
  366.     switch (numcolor) {
  367.         case 1:
  368.             numcolor = 2;
  369.             break;
  370.         case 2:
  371.             numcolor = 4;
  372.             break;
  373.         case 4:
  374.             numcolor = 16;
  375.             break;
  376.         case 8:
  377.             numcolor = 256;
  378.             break;
  379.         }
  380.     
  381.     myWindow = NewCWindow(nil, &BaseRect, "\p", TRUE, zoomDocProc, 
  382.                             (WindowPtr) -1, TRUE, 150);
  383.     SetPort((WindowPtr) myWindow);
  384.     DrawGrowIcon (myWindow);
  385.  
  386.     srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
  387.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  388.     
  389. /*______________________________________________________*/
  390. /*                    Set menus                         */
  391. /*______________________________________________________*/
  392.     mymenu0 = GetMenu(appleID);
  393.     AppendResMenu(mymenu0, 'DRVR');
  394.     InsertMenu(mymenu0,0);
  395.     mymenu1 = newmenu(129,"File");
  396.     appendmenu(mymenu1,"Start;-;Print;-;Quit");
  397.     InsertMenu(mymenu1,0);
  398.     mymenu2 = newmenu(130,"Edit");
  399.     InsertMenu(mymenu2,0);
  400.     DrawMenuBar();
  401.  
  402. /*______________________________________________________*/
  403. /*                  Init variables                      */
  404. /*______________________________________________________*/
  405.     DoneFlag = FALSE;
  406.     yieldTime = 0;
  407.     return;
  408. }
  409.  
  410. main()
  411. {
  412.     Boolean        track;
  413.     long        growResult;
  414. /*______________________________________________________*/
  415. /*                   Main Event loop                    */
  416. /*______________________________________________________*/
  417.     init();
  418.     for ( ;; ) {
  419.         if (DoneFlag) {
  420.             ExitToShell();
  421.             }
  422.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  423.             switch (myEvent.what) {
  424.                 case mouseDown:
  425.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  426.                         case inSysWindow:
  427.                             SystemClick(&myEvent, whichWindow);
  428.                             break;
  429.                         case inMenuBar:
  430.                             doCommand(MenuSelect(myEvent.where));
  431.                             break;
  432.                         case inContent:
  433.                             break;
  434.                         case inDrag:
  435.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  436.                             EraseRect (&whichWindow->portRect);
  437.                             DrawGrowIcon (whichWindow);
  438.                             break;
  439.                         case inGrow:
  440.                             growResult = GrowWindow (whichWindow, myEvent.where,
  441.                                                     &minRect);
  442.                             SizeWindow(whichWindow, LoWord(growResult), 
  443.                                     HiWord(growResult), TRUE);
  444.                             EraseRect (&whichWindow->portRect);
  445.                             DrawGrowIcon (whichWindow);
  446.                             break;
  447.                         case inGoAway:
  448.                             track = TrackGoAway (whichWindow, myEvent.where);
  449.                             if (track)
  450.                                 CloseWindow (whichWindow);
  451.                             break;
  452.                         case inZoomIn:
  453.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  454.                             if (track) {
  455.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  456.                                 EraseRect (&whichWindow->portRect);
  457.                                 DrawGrowIcon (whichWindow);
  458.                                 }
  459.                             break;
  460.                         case inZoomOut:
  461.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  462.                             if (track) {
  463.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  464.                                 EraseRect (&whichWindow->portRect);
  465.                                 DrawGrowIcon (whichWindow);
  466.                                 }
  467.                             break;
  468.                         default:
  469.                             break;
  470.                         }
  471.                     break;
  472.                 case keyDown:
  473.                     break;
  474.                 case autoKey:
  475.                     break;
  476.                 case updateEvt:
  477.                     if ((WindowPtr) myEvent.message == myWindow) {
  478.                         BeginUpdate((WindowPtr) myWindow);
  479.                         EndUpdate((WindowPtr) myWindow);
  480.                         }
  481.                     break;
  482.                 case diskEvt:
  483.                     break;
  484.                 case activateEvt:
  485.                     break;
  486.                 case app4Evt:
  487.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  488.                         yieldTime = 30;
  489.                         HideWindow((WindowPtr) myWindow);
  490.                         }
  491.                     else { /* Resume */
  492.                         yieldTime = 0;
  493.                         ShowWindow((WindowPtr) myWindow);
  494.                         SetPort((WindowPtr) myWindow);
  495.                         }
  496.                     break;
  497.                 default:
  498.                     break;
  499.                 }
  500.             }
  501.         }
  502. }